/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grupof.projectam;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.FromContainsFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.filter.PacketFilter;


/**
 *
 * @author fredi
 */
public class InstantMessage {
    
    private XMPPConnection con = null;
    private boolean connect=false;
    private boolean dconnect=true;
    private PacketFilter filter = null;

    public InstantMessage(){
        
    }
 
    //Login to GoogleTalk
    public boolean login(String name,String pass,String resource){
        try{
            ConnectionConfiguration config = new ConnectionConfiguration(
                "talk.google.com", 5222, "gmail.com");
            config.setSASLAuthenticationEnabled(true);
            config.setReconnectionAllowed(true);
            con = new XMPPConnection(config);
            con.connect();
            
            con.login(name, pass, resource);
            System.out.println(con.getUser());
            filter = new AndFilter(new PacketTypeFilter(Message.class), new FromContainsFilter(con.getUser()));
            con.createPacketCollector(filter);
            connect=true;
            dconnect=false;

        } catch (Throwable ex) {
            ex.printStackTrace();
            connect = false;
            dconnect=true;
        }
        return connect;
    }
    
    //Logout
    public boolean logout(){
        if (con != null) {
            con.disconnect();
            dconnect=true;
            connect=false;
        }
        return dconnect;
    }
}
